//
//  PS_SquareTwoLevelReplyViewController.m
//  FinanceSupermarket
//
//  Created by 丹丹 on 2018/12/21.
//  Copyright © 2018年 xuezi. All rights reserved.
//

#import "PS_SquareTwoLevelReplyViewController.h"
#import "PS_SquareDetailCell.h"
#import "PS_SquareDetailReplyCell.h"
#import "PS_ArticleDetailModel.h"
#import "IQKeyboardManager.h"
@interface PS_SquareTwoLevelReplyViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>{

    NSInteger pageNum;
    UITextField * searchTF;
    UIButton * sendButton;
    NSMutableAttributedString *placeholder;
}
@property (nonatomic, strong)UITableView * listTableView;
@property (nonatomic, strong)UIView * footerView;
@property (nonatomic, strong)NSMutableArray * commentArray;
@property (nonatomic, strong)UIView * bottomView;

@end

@implementation PS_SquareTwoLevelReplyViewController

- (UIView *)bottomView{
    
    if (_bottomView == nil) {
        
        _bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 50, SCREEN_WIDTH, 50)];
        _bottomView.backgroundColor = [UIColor whiteColor];
        
        searchTF =[[UITextField alloc]initWithFrame:CGRectMake(15, 7, SCREEN_WIDTH-105, 35)];
        searchTF.font = [UIFont systemFontOfSize:15];
        searchTF.textColor = [UIColor blackColor];
        searchTF.layer.borderWidth = 1;
        searchTF.layer.borderColor = [UIColor whiteColor].CGColor;
        searchTF.layer.cornerRadius = 15;
        searchTF.backgroundColor = [UIColor colorWithHexString:@"efefef"];
        searchTF.delegate = self;
        
        //文本框左视图
        UIView *leftView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 35, 35)];
        leftView.backgroundColor = [UIColor clearColor];
        
        UIImageView * searchLogo = [[UIImageView alloc] initWithFrame:CGRectMake(7, 0, 20, 35)];
        searchLogo.image = [UIImage imageNamed:@"xiepinglun"];
        searchLogo.contentMode = UIViewContentModeScaleAspectFit;
        [leftView addSubview:searchLogo];
        searchTF.leftView = leftView;
        searchTF.leftViewMode = UITextFieldViewModeAlways;
        
        NSString * placeholderStr = @"写评论";
        placeholder = [[NSMutableAttributedString alloc] initWithString:placeholderStr];
        [placeholder addAttribute:NSForegroundColorAttributeName
                            value:[UIColor lightGrayColor]
                            range:NSMakeRange(0, placeholderStr.length)];
        searchTF.attributedPlaceholder = placeholder;
        [_bottomView addSubview:searchTF];
    
        sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
        sendButton.frame = CGRectMake(SCREEN_WIDTH-75, 7, 60, 35);
        sendButton.layer.cornerRadius = 15;
        [sendButton setBackgroundColor:[UIColor colorWithHexString:@"f7c2dc"]];
        [sendButton setTitle:@"发送" forState:UIControlStateNormal];
        [sendButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [sendButton addTarget:self action:@selector(sendCommentAction:) forControlEvents:UIControlEventTouchUpInside];
        [_bottomView addSubview:sendButton];
        
        
    }
    
    return _bottomView;
    
}

- (NSMutableArray *)commentArray{
    
    if (_commentArray == nil) {
        
        _commentArray = [NSMutableArray array];
    }
    
    return _commentArray;
}
- (UIView *)footerView{
    
    if (_footerView == nil) {
        
        _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.listTableView.frame), SCREEN_WIDTH, 30)];
        _footerView.backgroundColor = RGBCOLOR(238, 238, 238);
        UILabel * lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 7, SCREEN_WIDTH, 15)];
        lable.text = @"已经到底了~~";
        lable.font = [UIFont systemFontOfSize:13];
        lable.textAlignment = NSTextAlignmentCenter;
        lable.textColor = RGBCOLOR(153, 153, 153);
        [_footerView addSubview:lable];
    }
    
    return _footerView;
}

- (UITableView *)listTableView{
    
    if (_listTableView == nil) {
        
        _listTableView = [[UITableView alloc]initWithFrame:CGRectMake(0,SafeAreaTopHeight, SCREEN_WIDTH,SCREEN_HEIGHT-SafeAreaBottomHeight-SafeAreaTopHeight-50)];
        _listTableView.dataSource = self;
        _listTableView.delegate = self;
        _listTableView.showsVerticalScrollIndicator= NO;
        _listTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _listTableView.scrollEnabled = YES;
        //        _listTableView.tableHeaderView = self.headerView;
        _listTableView.tableFooterView = self.footerView;
        
    }
    
    return _listTableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.nav_titleLabel.text = @"详情";
    [self.view addSubview:self.listTableView];
    [self.view addSubview:self.bottomView];
    pageNum = 1;
    [self getCommontList];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return 1;
        
    }else{
        
        return self.commentArray.count;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    self.listTableView.rowHeight = UITableViewAutomaticDimension;
    self.listTableView.estimatedRowHeight = 44;
    return self.listTableView.rowHeight;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 30;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return 0;
        
    }else{
        
        return 55;
    }
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView * backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 55)];
    backView.backgroundColor = [UIColor whiteColor];
    
    UIView * line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
    line.backgroundColor = [UIColor colorWithHexString:@"efefef"];
    [backView addSubview:line];
    
    
    UILabel * markLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 20, 100, 25)];
    markLabel.text = @"全部评价";
    markLabel.userInteractionEnabled = YES;
    [backView addSubview:markLabel];
    
    //    [backView addSubview:arrowIamge];
    
    UIView * line2 = [[UIView alloc]initWithFrame:CGRectMake(15,54, SCREEN_WIDTH-30, 1)];
    line2.backgroundColor = [UIColor colorWithHexString:@"efefef"];
    [backView addSubview:line2];
    return backView;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (indexPath.section == 0) {
        
        
            PS_SquareDetailCell * cell = [tableView dequeueReusableCellWithIdentifier:@"PS_SquareDetailCell"];
            if (cell == nil) {
                
                cell = [[[NSBundle mainBundle]loadNibNamed:@"PS_SquareDetailCell" owner:self options:nil]objectAtIndex:0];
                
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.height_titleLabel.constant = 0;
            cell.top_contentLabel.constant = 75;
            cell.top_headIamge.constant = 15;
            cell.zanBtn.hidden = NO;
            [cell.zanBtn setTitle:_detailModel.praise_points forState:UIControlStateNormal];
            [cell.zanBtn addTarget:self action:@selector(zanAction:) forControlEvents:UIControlEventTouchUpInside];
            [cell setDataForCell:_detailModel];
            return cell;
        
    }else{
        
        PS_SquareDetailReplyCell * cell = [tableView dequeueReusableCellWithIdentifier:@"PS_SquareDetailReplyCell"];
        if (cell == nil) {
            
            cell = [[[NSBundle mainBundle]loadNibNamed:@"PS_SquareDetailReplyCell" owner:self options:nil]objectAtIndex:0];
            
        }
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.zanBTn.hidden = YES;
        cell.commentBtn.hidden = YES;
        PS_ArticleDetailModel * model = [self.commentArray objectAtIndex:indexPath.row];
        [cell setDataForCell:model];
        return cell;
        
    }
    
}
- (void)zanAction:(UIButton *)sender{
    
    
}
- (void)sendCommentAction:(UIButton *)sender{
    
    [searchTF resignFirstResponder];
    [self getReplyData];
    searchTF.text = @"";
}

#pragma uitextfiledDelegate

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    
    searchTF.frame = CGRectMake(15, 7, SCREEN_WIDTH-105, 35);
    searchTF.backgroundColor = [UIColor whiteColor];
    //    searchTF.placeholder = @"写评论";
    self.bottomView.backgroundColor = [UIColor colorWithHexString:@"efefef"];
    
    sendButton.hidden = NO;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    //TODO: 页面appear 禁用
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [IQKeyboardManager sharedManager].enableAutoToolbar = YES;
}

- (void)getReplyData{
    
    if (searchTF.text.length <2 ||searchTF.text.length>200) {
        
        [MBProgressHUD showError:@"请输入内容在2-200字之间" toView:self.view];
        return;
    }
    
    NSString * uid;
    if (USERID) {
        uid = USERID;
    }else{
        uid = @"";
    }
    [Utils post:ADDREPLYCOMMONT parameters:@{@"uid":USERID,@"id":_detailModel.arcticleID,@"cid":_model.arcticleID,@"content":searchTF.text} success:^(NSInteger recode, id jsonObject) {
        
        [self getCommontList];
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
}

- (void)getCommontList{
    
    
    [Utils post:REPLYCOMMONTTWO parameters:@{@"id":_model.arcticleID,@"page":@(pageNum)} success:^(NSInteger recode, id jsonObject) {
        
        NSArray * listArray = [jsonObject objectForKey:@"data"];
        [self.commentArray removeAllObjects];
        for (NSDictionary * dic in listArray) {
            
            PS_ArticleDetailModel * model = [PS_ArticleDetailModel mj_objectWithKeyValues:dic];
            model.arcticleID = [dic objectForKey:@"id"];
            [self.commentArray addObject:model];
            
        }
        
        [self.listTableView reloadData]; 
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
}

@end
